package com.bjpowernode.services.impls;

import com.alibaba.excel.EasyExcel;
import com.bjpowernode.constants.Constant;
import com.bjpowernode.entities.TClue;
import com.bjpowernode.entities.TCustomer;
import com.bjpowernode.excels.CustomerExcel;
import com.bjpowernode.mappers.TClueMapper;
import com.bjpowernode.mappers.TCustomerMapper;
import com.bjpowernode.querys.BaseQuery;
import com.bjpowernode.querys.CustomerQuery;
import com.bjpowernode.services.CustomerService;
import com.bjpowernode.utils.LoginInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private TClueMapper tClueMapper;
    @Resource
    private TCustomerMapper tCustomerMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean convertCustomer(CustomerQuery customerQuery) {
        TClue tClue = tClueMapper.selectById(customerQuery.getClueId());
        if (tClue.getState() == -1) {
            throw new RuntimeException("该线索已经转过客户，不能再转了.");
        }
        TCustomer tCustomer = new TCustomer();
        BeanUtils.copyProperties(customerQuery, tCustomer);
        tCustomer.setCreateTime(new Date());
        tCustomer.setCreateBy(LoginInfoUtil.getCurrentLoginUser().getId());
        int insert = tCustomerMapper.insertSelective(tCustomer);
        TClue upClue = new TClue();
        upClue.setId(customerQuery.getClueId());
        upClue.setState(-1);
        int update = tClueMapper.updateByIdSelective(upClue);
        return insert >= 1 && update >= 1;
    }

    @Override
    public PageInfo<TCustomer> getCustomersByPage(Integer current) {
        PageHelper.startPage(current, Constant.PAGE_SIZE);
        List<TCustomer> tCustomerList = tCustomerMapper.selectByPage(new BaseQuery(), null);
        return new PageInfo<>(tCustomerList);
    }

    @Override
    public void exportExcel(OutputStream outputStream, List<String> idList) {
        List<TCustomer> tCustomerList = tCustomerMapper.selectByPage(new BaseQuery(), idList);
        List<CustomerExcel> customerExcelList = new ArrayList<>();
        for (TCustomer tCustomer : tCustomerList) {
            CustomerExcel customerExcel = new CustomerExcel();
            customerExcel.setOwnerName(ObjectUtils.isEmpty(tCustomer.getOwnerDO()) ? "" : tCustomer.getOwnerDO().getName());
            customerExcel.setActivityName(ObjectUtils.isEmpty(tCustomer.getActivityDO()) ? "" : tCustomer.getActivityDO().getName());
            customerExcel.setFullName(ObjectUtils.isEmpty(tCustomer.getClueDO()) ? "" : tCustomer.getClueDO().getFullName());
            customerExcel.setAppellationName(ObjectUtils.isEmpty(tCustomer.getAppellationDO()) ? "" : tCustomer.getAppellationDO().getTypeValue());
            customerExcel.setPhone(ObjectUtils.isEmpty(tCustomer.getClueDO()) ? "" : tCustomer.getClueDO().getPhone());
            customerExcel.setWeixin(ObjectUtils.isEmpty(tCustomer.getClueDO()) ? "" : tCustomer.getClueDO().getWeixin());
            customerExcel.setNeedLoanName(ObjectUtils.isEmpty(tCustomer.getNeedLoanDO()) ? "" : tCustomer.getNeedLoanDO().getTypeValue());
            customerExcel.setIntentionStateName(ObjectUtils.isEmpty(tCustomer.getIntentionStateDO()) ? "" : tCustomer.getIntentionStateDO().getTypeValue());
            customerExcel.setSourceName(ObjectUtils.isEmpty(tCustomer.getSourceDO()) ? "" : tCustomer.getSourceDO().getTypeValue());
            customerExcel.setIntentionProductName(ObjectUtils.isEmpty(tCustomer.getIntentionProductDO()) ? "" : tCustomer.getIntentionProductDO().getName());
            customerExcel.setNextContactTime(tCustomer.getNextContactTime());
            customerExcelList.add(customerExcel);
        }
        EasyExcel.write(outputStream, CustomerExcel.class).sheet().doWrite(customerExcelList);
    }

    @Override
    public TCustomer getCustomerById(Integer id) {
        return tCustomerMapper.selectById(id);
    }
}
